import 'package:get/get.dart';
import 'package:sports_new/server/api/coupon_api.dart';
import '../../../entity/other/resource_entity.dart';
import '../../../entity/pay/coupon_pack_entity.dart';
import '../../../entity/pay/recharge_entity.dart';
import '../../../server/api/other_api.dart';
import '../../../server/api/pay_api.dart';
import '../../../utils/routes.dart';
import '../../../utils/toast_utils.dart';
import '../../../utils/user.dart';
import '../../../utils/utils.dart';
import '../../../widgets/pay_bottom_sheet.dart';
import '../../service/resource_service.dart';

class CouponPackLogic extends GetxController{
  List<ResourceEntity>? carousel;
  List<CouponPackEntity>? entity;

  @override
  void onReady() {
    Utils.onEvent("wd_czqb");
    getData();
    super.onReady();
  }

  Future getData() async{
    carousel = Get.find<ResourceService>().lbt.where((element) => element.button == "coupon_pack").toList();
    entity = await CouponApi.getCouponPack();
    update();
  }

  Future toPay(CouponPackEntity entity) async{
    Utils.onEvent('center_coupon_buy');
    double money = entity.price ?? 0;
    final PayResult? result = await Get.bottomSheet(
      isScrollControlled: true,
      PayBottomSheet(
        payInfo: PayInfo(
          price: money.toStringAsFixed(2),
          payScene: PayScene.couponPack,
          buyId: "${entity.id}"
        )
      )
    );
    if(result != null && result.toPay == PayStatus.goldPay){
      var s = await PayApi.couponPackOrder(entity.id);
      if(s == true) {
        getData();
        Utils.onEvent('pay_done');
        if(Get.previousRoute.contains(Routes.myCoupons)){
          Get.back();
        }else{
          Get.toNamed(Routes.myCoupons);
        }
      }else{
        Utils.onEvent('aizh_zfyd_qxzf');
      }
    }else if(result?.toPay == PayStatus.directPay){
      getData();
      Utils.onEvent('aizh_zfyd_qrzf');
      if(Get.previousRoute.contains(Routes.myCoupons)){
        Get.back();
      }else{
        Get.toNamed(Routes.myCoupons);
      }
    }else{
      Utils.onEvent('aizh_zfyd_qxzf');
      ToastUtils.show("购买失败");
    }
    User.fetchUserInfo(fetchFocus: false);
  }
}